#include "DbCRUDService.h"
#include "../../tools/DbCRUD/DbDelete.h"// 包含 DbDelete 工具类
#include <iostream>

using grpc::Status;
using grpc::StatusCode;

// --- 删除 (Delete) ---
Status DbCRUDService::DeleteUser(
    ServerContext* context,
    const DeleteUserRequest* request,
    DeleteUserResponse* response) {
    
    int32_t user_id = request->user_id();

    std::string error_message;
    if (DbDelete::DeleteUser(user_id, error_message)) {
        response->set_success(true);
        std::cout << "Successfully deleted user with ID: " << user_id << std::endl;
    } else {
        response->set_success(false);
        response->set_error_message(error_message);
        std::cerr << "Failed to delete user with ID: " << user_id 
                  << ". Error: " << error_message << std::endl;
        // 可以根据 error_message 的内容决定返回不同的 gRPC StatusCode
        // 例如，如果是数据库连接失败，可以返回 UNAVAILABLE
        // 这里为简化，统一返回 OK 并在 response 中携带错误信息
    }

    return Status::OK;
}

Status DbCRUDService::DeleteList(
    ServerContext* context,
    const DeleteListRequest* request,
    DeleteListResponse* response) {
    
    int32_t list_id = request->list_id();

    std::string error_message;
    if (DbDelete::DeleteList(list_id, error_message)) {
        response->set_success(true);
        std::cout << "Successfully deleted list with ID: " << list_id << std::endl;
    } else {
        response->set_success(false);
        response->set_error_message(error_message);
        std::cerr << "Failed to delete list with ID: " << list_id 
                  << ". Error: " << error_message << std::endl;
    }

    return Status::OK;
}

Status DbCRUDService::DeleteTodo(
    ServerContext* context,
    const DeleteTodoRequest* request,
    DeleteTodoResponse* response) {
    
    int32_t todo_id = request->todo_id();

    std::string error_message;
    if (DbDelete::DeleteTodo(todo_id, error_message)) {
        response->set_success(true);
        std::cout << "Successfully deleted todo with ID: " << todo_id << std::endl;
    } else {
        response->set_success(false);
        response->set_error_message(error_message);
        std::cerr << "Failed to delete todo with ID: " << todo_id 
                  << ". Error: " << error_message << std::endl;
    }

    return Status::OK;
}

Status DbCRUDService::DeleteComment(
    ServerContext* context,
    const DeleteCommentRequest* request,
    DeleteCommentResponse* response) {
    
    int32_t comment_id = request->comment_id();

    std::string error_message;
    if (DbDelete::DeleteComment(comment_id, error_message)) {
        response->set_success(true);
        std::cout << "Successfully deleted comment with ID: " << comment_id << std::endl;
    } else {
        response->set_success(false);
        response->set_error_message(error_message);
        std::cerr << "Failed to delete comment with ID: " << comment_id 
                  << ". Error: " << error_message << std::endl;
    }

    return Status::OK;
}

Status DbCRUDService::DeleteEntity(
    ServerContext* context,
    const DeleteEntityRequest* request,
    DeleteEntityResponse* response) {
    
    // 注意：Entity 使用的是字符串 ID
    const std::string& entity_id = request->entity_id();

    std::string error_message;
    if (DbDelete::DeleteEntity(entity_id, error_message)) {
        response->set_success(true);
        std::cout << "Successfully deleted entity with ID: " << entity_id << std::endl;
    } else {
        response->set_success(false);
        response->set_error_message(error_message);
        std::cerr << "Failed to delete entity with ID: " << entity_id 
                  << ". Error: " << error_message << std::endl;
    }

    return Status::OK;
}